import { routes } from "../router";
import { GET_ROLE_LIST } from "./constants";

export interface RoleListAction {
  type: string;
  payload: {
    routesList: RoutesList;
  };
}

export interface RoutesList {
  password: string;
  role: string;
  router: RouterItem[];
}


export interface RouterItem {
  key: string;
  icon?: React.ReactNode;
  label?: string;
  children?: RouterItem[];
}

export interface RoleListState {
  routesList: RoutesList;
}

const initialState: RoleListState = {
  routesList: {
    password: "",
    role: "",
    router: [] as RouterItem[],
  },
};

export default function reducer(state = initialState, action: RoleListAction) {
  switch (action.type) {
    case GET_ROLE_LIST:
      const list = [...routes, ...action.payload.routesList?.router] as RouterItem[];
      const routesList = {
        ...action.payload.routesList,
        router: list,
      }
      localStorage.setItem('role', JSON.stringify(routesList));

      return {
        ...state,
        routesList,
      };
    default:
      return state;
  }
}
